<?php

namespace App\Admin\Controllers;

use App\Models\TaskAudit;
use App\Models\Task;
use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;
use Illuminate\Http\Request;
use App\Admin\Extensions\Success;
use App\Admin\Extensions\Failed;


class TaskAuditController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('任务审核')
            ->description('任务审核')
            ->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $total = TaskAudit::count();
                    $infoBox = new InfoBox('提交任务总数', 'tasks', 'aqua', '/admin/taskaudit', $total);
                    $column->append($infoBox);
                });
                $row->column(3, function (Column $column) {
                    $today_total = TaskAudit::whereDate('created_at', date('Y-m-d'))->count();
                    $infoBox = new InfoBox('今日新提交任务', 'tasks', 'aqua', '', $today_total);
                    $column->append($infoBox);
                });

            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new TaskAudit);
        $grid->model()->wherein('status', [0, 3]);
        $grid->id('Id');
        $grid->task_id('任务名称')->display(function($task_id){
            $data = Task::where('id',$task_id)->first();
            if ($data) {
                return $data->name;
            }
        });
        $grid->user_id('用户昵称')->display(function($user_id){
            $data = User::where('id',$user_id)->first();
            if ($data) {
                return $data->name;
            }
        });
        $grid->column('手机号')->display(function($user_id){
            $data = User::where('id',$this->user_id)->first();
            if ($data) {
                return $data->phone;
            }
        });

        $grid->status('任务状态')->display(function($status){
            switch ($status) {
                case '0':
                    return "<span class='label label-warning'>未审核</span>";
                    break;
                case '1':
                    return "<span class='label label-primary'>通过未打款</span>";
                    break;
                case '2':
                    return "<span class='label label-success'>通过已打款</span>";
                    break;
                case '3':
                    return "<span class='label label-danger'>未通过</span>";
                    break;
                default:
                    # code...
                    break;
            }
        });
        $grid->created_at('创建时间');
        $grid->updated_at('更新时间');
        $grid->disableCreateButton();
        $grid->disableRowSelector();
        
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableEdit();
            $actions->disableView();            
            $row = $actions->row;
            if($row['status'] == 0){
                // append一个操作
                $actions->append("<a href='/admin/taskdetail/" . $actions->getKey() . "'>" . '<button type="button" class="btn btn-primary">任务详情</button>' . "</a>");
                $actions->append(new Success($actions->getKey()));
                $actions->append(new Failed($actions->getKey()));
                                  
            }         
        });
        
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('status', '任务状态')->radio([
                0 => '未审核',
                1 => '通过未打款',
                2 => '通过已打款',
                3 => '未通过',
            ]);
            $filter->scope('未审核')->where('status', 0);
            $filter->scope('今日新增', '今日新增')->whereDate('created_at', date('Y-m-d'));

        });

        return $grid;
    }
    //任务详情
    public function taskdetail(Request $request,$id)
    {   
        $audit = TaskAudit::findOrFail($id);
        $detail = json_decode($audit->detail);
        $headers = ['id', '标题', '内容'];

        $ret = [];
        foreach ($detail as $v) {
            if($v->type ==1){
                $content = $v->content;
            }else{
                $src = "/uploads/".$v->content;
                $content = "<a href='".$src."' target='_blank'><img src='".$src."' style='max-width:100px;max-height:100px' class='img img-thumbnail'></a>";
            }
            $ret[] = [$v->id, $v->title, $content];
        }
        $table = new Table($headers, $ret);

        $box = new Box('任务详情', $table->render());
        $box->style('success');
        $box->solid();
        $box->collapsable();
        $box->back();
        $content = new Content();        
        return $content
            ->header('任务详情')
            ->description('详情')
            ->body($box->render());
    }
    //审核通过
    public function success(Request $request)
    {       
        $id = $request->id;
        $audit = TaskAudit::findOrFail($id);
        $audit->status = 1;
        $res = $audit->save();
        if($res){
            return $this->ajax_success('操作成功');
        }else{
            return $this->ajax_error('操作失败');
        }
        
    }
    //审核不通过
    public function failed(Request $request)
    {       
        $id = $request->id;
        $audit = TaskAudit::findOrFail($id);
        $audit->status = 3;
        $audit->reason = $request->reason;
        $res = $audit->save();
        if($res){
            return $this->ajax_success('操作成功');
        }else{
            return $this->ajax_error('操作失败');
        }
        
    }


}
